package com.duoduo.admin.entity;


import java.io.Serializable;
import java.util.Objects;

import org.hibernate.Hibernate;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;


@Entity
@Table(name = "sys_role_menu")
@IdClass(SysRoleMenu.RoleMenuId.class)
@Getter
@Setter
@ToString
public class SysRoleMenu {
	/**
	 * 角色ID
	 */
	@Id
	@Column(name = "role_id", nullable = false)
	private Long roleId;

	/**
	 * 菜单ID
	 */
	@Id
	@Column(name = "menu_id", nullable = false)
	private Long menuId;
	


	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "menu_id", nullable = false, insertable = false, updatable = false)
	@ToString.Exclude
	private SysMenu menu;

	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "role_id", nullable = false, insertable = false, updatable = false)
	@ToString.Exclude
	private SysRole role;

	@Data
	public static class RoleMenuId implements Serializable {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = -1947048125991327930L;

		private Long menuId;

		private Long roleId;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o))
			return false;
		SysRoleMenu roleMenu = (SysRoleMenu) o;
		return menuId != null && Objects.equals(menuId, roleMenu.menuId) && roleId != null
				&& Objects.equals(roleId, roleMenu.roleId);
	}

	@Override
	public int hashCode() {
		return Objects.hash(menuId, roleId);
	}
}
